home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / ai.prl / mike1.exe / DEDUCE.KB < prev    next >
Encoding:
Text File  |  1990-07-11  |  3.5 KB  |  122 lines

  1. /* DEDUCE.KB
  2.  
  3. Code to put backward-chaining rule tracer through its paces.
  4. To try it, reconsult this file, and invoke tracing options 7 and 8
  5. as follows:
  6.    ?- tracing([7,8]).
  7. Or else just invoke
  8.    ?- tracing.
  9. and respond to the menu of choices, paying particular attention to
  10. options 7 and 8.
  11.  
  12. Then, invoke MIKE's backward-chainer as follows:
  13.  
  14.    ?- deduce [What, is, fun].
  15.  
  16. ----------------------------------------------------------------------
  17. An alternative, forward-chaining proof of the same conclusion uses
  18. rule 'fun3' defined below.  It is invoked by typing
  19.  
  20.     ?- go.
  21.  
  22. (Do not use the normal ?- fc.  as explained in the comment below)
  23. -----------------------------------------------------------------------
  24. */
  25.  
  26.  
  27. /* Rules 'fun1' and 'fun2' correspond intuitively to the notion that
  28. "Red cars or blue bikes are fun".  To deduce that something is fun, it
  29. is therefore sufficient to deduce that the thing in question is both red
  30. and that it is a car. Alternatively, it is sufficient to deduce that the
  31. thing in question is both blue and that it is a bike */
  32.  
  33. rule fun1 backward
  34.   if
  35.     [X,is,red] &
  36.     [X,isa,car]
  37.   then
  38.     [X,is,fun].
  39.  
  40. rule fun2 backward
  41.   if
  42.     [X,is,blue] &
  43.     [X,isa,bike]
  44.   then
  45.     [X,is,fun].
  46.  
  47.  
  48. /* ============= alternative forward-chaining variation ================ */
  49.  
  50. /* N.B.  DO NOT USE  ?- fc. TO TEST THIS OUT!!!   USE  ?- go.  INSTEAD.
  51. THE REASON IS THAT WORKING MEMORY IS MANUALLY 'SEEDED' AT THE END OF THIS
  52. FILE, AND ?- fc. WOULD CLEAR IT OUT (look at the definition of fc to see why)
  53. */
  54.  
  55. rule fun3 forward
  56.  if
  57.    [X, is, red] & [X, isa, car] or    /* more intuitive use of disjunction */
  58.    [X, is, blue] & [X, isa, bike]
  59.  then
  60.    add [X, is, fun] &
  61.    announce ['I have concluded what is fun by forward-chaining: ', X] &
  62.    halt.
  63.  
  64. /* Seed working memory when this file is loaded: */
  65.  
  66.  
  67.  
  68. ?- initialise.          /* clear working memory first */
  69. ?- add [block1,is,red].
  70. ?- add [block2,is,red].
  71. ?- add [block3,is,red].
  72. ?- add [car1,isa,car].
  73. ?- add [car2,isa,car].
  74. ?- add [house1,is,blue].
  75. ?- add [triangle39,is,blue].
  76. ?- add [bike3,is,blue].
  77. ?- add [bike1,isa,bike].
  78. ?- add [bike2,isa,bike].
  79. ?- add [bike3,isa,bike].
  80.  
  81. ?- nl, write('If you decide to use forward chaining to test DEDUCE.KB'),nl,
  82.    write('be sure to use  ?- go.  rather than  ?- fc.'),nl,
  83.    write('because working memory has already been "seeded" manually.'),nl.
  84.  
  85. /* notice that it would have been just as possible to have an 'initialisation
  86. rule' which looked like this:
  87.  
  88. (remember, the next two rules are enclosed in this large comment....)
  89.  
  90. rule begin forward
  91.  if
  92.    start
  93.  then
  94.    remove start &
  95.    add [block1,is,red] &
  96.    add [block2,is,red] &
  97.    add [block3,is,red]
  98.    <etc. etc.>
  99.  
  100. rule make_deduction forward
  101.  if
  102.    deduce [X,is,fun]
  103.  then
  104.    announce ['I have just deduced what is fun: ', X] &
  105.    halt.
  106.  
  107. There are thus two styles of usage:
  108.   (a) you can seed working memory 'manually', as above, and invoke
  109. 'deduce' yourself from top-level;
  110.   (b) you can seed working memory on the right-hand side of a rule, as
  111. in the commented-out rule above, and use a second rule to invoke
  112. 'deduce' within a forward-chaining sequence.
  113.  
  114. Which style you use is a matter of personal taste.
  115.  
  116. If you opt for the latter style, you can copy and paste the rules
  117. 'begin' and 'make_deduction' (you must of course complete the
  118. definition of 'begin'), so that they reside contiguously with the
  119. other rules in this file.
  120.  
  121. */
  122.